﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Network
{
    using System;
    using System.Threading;

    public class ServerPerformanceCounter : IDisposable
    {
        private Timer perSecondCounterTimer;

        internal DateTime serverStartDateTime;
        public DateTime ServerStartDateTime
        {
            get
            {
                return serverStartDateTime;
            }
        }

        internal long totalBytesReceived;
        public long TotalBytesReceived
        {
            get
            {
                return totalBytesReceived;
            }
        }

        private long lastSecondBytesReceived;
        private long perSecondBytesReceived;
        public long PerSecondBytesReceived
        {
            get
            {
                return perSecondBytesReceived;
            }
        }

        internal long totalBytesSent;
        public long TotalBytesSent
        {
            get
            {
                return totalBytesSent;
            }
        }

        internal long totalSentCount;
        public long TotalSentCount
        {
            get
            {
                return totalSentCount;
            }
        }

        private long lastSecondSentCount;
        private long perSecondSentConut;
        public long PerSecondSentConut
        {
            get
            {
                return perSecondSentConut;
            }
        }

        internal long totalReceiveCount;
        public long TotalReceiveCount
        {
            get
            {
                return totalReceiveCount;
            }
        }

        private long lastSecondReceiveCount;
        private long perSecondReceiveConut;
        public long PerSecondReceiveConut
        {
            get
            {
                return perSecondReceiveConut;
            }
        }

        private long lastSecondBytesSent;
        private long perSecondBytesSent;
        public long PerSecondBytesSent
        {
            get
            {
                return perSecondBytesSent;
            }
        }

        public ServerPerformanceCounter()
        {
            perSecondCounterTimer = new Timer(state =>
            {
                perSecondBytesReceived = totalBytesReceived - lastSecondBytesReceived;
                perSecondBytesSent = totalBytesSent - lastSecondBytesSent;
                perSecondSentConut = totalSentCount - lastSecondSentCount;
                perSecondReceiveConut = totalReceiveCount - lastSecondReceiveCount;
                lastSecondBytesSent = totalBytesSent;
                lastSecondBytesReceived = totalBytesReceived;
                lastSecondReceiveCount = totalReceiveCount;
                lastSecondSentCount = totalSentCount;
            }, null, 0, 1000);
        }

        public void Dispose()
        {
            perSecondCounterTimer.Dispose();
        }
    }
}
